#include "Logic.h"
#include "Menu.h"

#include "../Graphic/Graphic.h"

int Logic_ID = 0;
bool Logic_Tile = false;

/** TEST */
int Logic_Test(int Data) {
    // Stuff happens
    logDebug("Hit Menu with Data '%d'", Data);
}
int Logic_Test_Graphic(int X, int Y, char *Text, int Data) {
    Graphic_LoadTile(Data);
    Graphic_Draw( (int)X, (int)Y, 2, true);
}
/** TEST END */
/** Menu Logic init */
int Logic_Init() {
    int i;

    /* Menü */
    Menu_Create( 600, 30, true, 0, &Logic_Icon, &Logic_Icon_Graphic);
    /* Lebensanzeige */
    Menu_Create( 0, 0, true, 1, &Logic_Header, &Logic_Header_Graphic);

    /* Herze */
    for(i = 0; i < 8; i++)
        Menu_Create( 20+(i*34), 20, true, i+1, &Logic_Live, &Logic_Live_Graphic);

    if(Configuration_GetIni( "logic:editor", false)) {
        /* Fenster für Tiles offen */
        Logic_Tile = true;
    int X = 0, Y = 0;
    G_Rect Border = { 10, 80, 0, 95 };
    for(i = 1; i < Graphic_Tile+1; i++) {
        Menu_Create( Y+16+Border.X, X+16+Border.Y, true, i, &Logic_MenuTile, &Logic_MenuTile_Graphic);
        Y += 34;
        while( Y > Border.H) {
            Y = 0;
            X+=34;
        }
    }
    }
}
/** Lebensanzeige */
int Logic_iLive = 15;
int Logic_Live_Graphic(int X, int Y, char *Text, int Data) {
    int i = 3;
    if(Data*2-1 <= Logic_iLive)
        i = 2;
    if(Data <= Logic_iLive/2)
        i = 1;
    Graphic_LoadIcon(i);
    Graphic_SetRect(32,32);
    Graphic_Fixed();
    Graphic_Draw( (int)X, (int)Y, 11, true);
    return 0;
}
int Logic_Live(int Data) {
    return 0;
}
/** Header */
/**********************************************************************************************/
int Logic_Header_Graphic(int X, int Y, char *Text, int Data) {
    Graphic_LoadFrame(Data);
    Graphic_Fixed();
    Graphic_Draw( (int)X, (int)Y, 10, false);
    return 0;
}
int Logic_Header(int Data) {
    return 0;
}
/** Option */
/**********************************************************************************************/
bool Option_Open = false;
int Logic_Icon_Graphic(int X, int Y, char *Text, int Data) {
    Graphic_LoadFrame(0);
    Graphic_SetRect(60,60);
    Graphic_Fixed();
    Graphic_Draw( (int)X, (int)Y, 10, true);
    Graphic_LoadIcon(Data);
    Graphic_SetRect(32,32);
    Graphic_Fixed();
    Graphic_Draw( (int)X, (int)Y, 11, true);
    if(Data != 0 && !Option_Open) {
        return 1;
    }
    return 0;
}

int Logic_Icon(int Data) {
    int i;
    switch(Data) {
        case 0:
            if(!Option_Open) {
                Option_Open = true;
                for(i = 0; i < 4; i++)
                    Menu_Create( 600, 80+(i*50), true, 4+i, &Logic_Icon, &Logic_Icon_Graphic);
                return 0;
            }
            else
                Option_Open = false;
        break;
        case 5:
            World_Load(0);
        break;
        case 4:
            World_Save(0);
        break;
        case 6:
            Graphic_ToggleFullscreen();
            Graphic_CalcZoom();
        break;
        case 7:
            main_Quit();
        break;
    }
    Option_Open = false;
    return 0;
}
/** Build */
/**********************************************************************************************/
int Logic_MenuTile_Graphic(int X, int Y, char *Text, int Data) {
    Graphic_LoadFrame(0);
    Graphic_SetRect(34,34);
    Graphic_Fixed();
    Graphic_Draw( (int)X, (int)Y, 10, true);
    if(Data >= Graphic_Tile)
        return 0;
    Graphic_LoadTile(Data);
    Graphic_SetRect(32,32);
    Graphic_Fixed();
    Graphic_Draw( (int)X, (int)Y, 11, true);
    return 0;
}

int Logic_MenuTile(int Data) {
    Logic_ID = Data;
    if(Data >= Graphic_Tile)
        Logic_ID = 0;
    return 0;
}

void Logic_Build(int X, int Y, int Layer) {
    World_SetTile(X,Y, Layer, Logic_ID);
}
